#!/bin/sh

#-------------------------------------#
#  SGT (Software Generation Toolkit)  #
#-------------------------------------#
#                                     #
#        Zip all files in \out        #
#                                     #
#  GDE 17/03/2004 v1.8                #
#       Add *.cpp files in archive    #
#       Fix OS detection              #
#                                     #
#  GDE 03/07/2003 v1.7                #
#       check \MAK subdirectory       #
#                                     #
#  GDE 22/04/2003 v1.6                #
#       Add register in misc.zip      #
#                                     #
#  GDE 12/12/2002 v1.5                #
#       Add files in misc.zip         #
#                                     #
#  GDE 25/09/2002 v1.4                #
#       Update for SGT 1.1.4          #
#                                     #
#  GDE 26/08/2002 v1.3                #
#       Modify the Zip path           #
#                                     #
#  GDE 07/05/2002 v1.2                #
#       update for SGT 1.1.0          #
#                                     #
#  GDE 25/03/2002 v1.1                #
#       update file formats           #
#       to archive                    #
#                                     #
#  GDE 20/12/2001 v1.0                #
#       creation                      #
#                                     #
#-------------------------------------#
# $1: specific option                 #
#-------------------------------------#



#-------------------------------------#
# Script informations
#-------------------------------------#
export SCRIPT_SGT_VER=1.8
export SCRIPT_SGT_DATE=17/03/2004
export SCRIPT_SGT_USER=GDE
export SCRIPT_SGT_NAME=zip_src
export SCRIPT_SGT_USAGE="Make archives files (*.zip) in output directory"
#-------------------------------------#



#---------------------
# Create and go to output directory
#---------------------
if [ -d mak ]
then
	mkdir -p -m775 out
	cd out
else
	export MSG_ERROR_SCRIPT=0
	export CMD_SCRIPT=Error
fi
#---------------------



# default value
export ZIP_ALL_LIB=OFF

#-------------------------------------#
# Parameters section
#-------------------------------------#
#---------------------
# Parameter 1 -> target file / specific option
#---------------------
if [ ! -z $1 ] && [ -z $CMD_SCRIPT ]
then
	if [ $1 = -v ]
	then
		export CMD_SCRIPT=Version
	else
		if [ $1 = -h ]
		then
			export CMD_SCRIPT=Help
		else
			if [ $1 = --help ]
			then
				export CMD_SCRIPT=Help
			else
				if [ $1 = -hm ]
				then
					export CMD_SCRIPT=More
				else
					if [ $1 = -u ]
					then
						export CMD_SCRIPT=Usage
					else
						if [ $1 = -lib ]
						then
							export ZIP_ALL_LIB=ON
						else
							export MSG_ERROR_SCRIPT=1
							export CMD_SCRIPT=Error
						fi
					fi
				fi
			fi
		fi
	fi
fi



#-------------------------------------#
# Process section
#-------------------------------------#
#---------------------
# Zip processus
#---------------------
if [ -z $CMD_SCRIPT ]
then
	export LIB_ZIP_FILES='*.lib *.LIB'
	export MISC_ZIP_FILES='__register__.* __e2p__*.* e2p.h e2p_csn.* *.lst m m64k s s64k ysc.* *.axf *.gpp'
	export SOURCES_ZIP_FILES='*.c *.cpp *.asm *.h* *.i *.sce *.txt *.csn'
	export OBJECTS_ZIP_FILES='*.o *.obj *.log *.dep *.flt *.src'

	echo " "
	echo "    $SCRIPT_SGT_USAGE"
	echo " "
	
	#---------------------
	# remove old log file if exist
	#---------------------
	rm -f zip_status.tmp
	rm -f zip_status.log
		
	if [ $TERM = cygwin ]
	then
		if [ $ZIP_ALL_LIB = ON ]
		then
			echo " Making libraries.zip ..."
			echo " Making libraries.zip ..." >> zip_status.tmp
			$SGT_DIR/tools/$TERM/zip/infozip2.3/zip.exe -m libraries $LIB_ZIP_FILES >> zip_status.tmp
		fi

		echo " Making misc.zip ..."
		echo " Making misc.zip ..." >> zip_status.tmp
		$SGT_DIR/tools/$TERM/zip/infozip2.3/zip.exe -m misc $MISC_ZIP_FILES >> zip_status.tmp

		echo " Making sources.zip ..."
		echo " Making sources.zip ..." > zip_status.tmp
		$SGT_DIR/tools/$TERM/zip/infozip2.3/zip.exe -m sources $SOURCES_ZIP_FILES >> zip_status.tmp
		
		echo " Making objects.zip ..."
		echo " Making objects.zip ..." >> zip_status.tmp
		$SGT_DIR/tools/$TERM/zip/infozip2.3/zip.exe -m objects $OBJECTS_ZIP_FILES >> zip_status.tmp		
	else
		if [ $ZIP_ALL_LIB = ON ]
		then
			echo " Making libraries.zip ..."
			echo " Making libraries.zip ..." >> zip_status.tmp
			/usr/local/bin/zip -m libraries $LIB_ZIP_FILES >> zip_status.tmp
		fi
		echo " Making misc.zip ..."
		echo " Making misc.zip ..." >> zip_status.tmp
		/usr/local/bin/zip -m misc $MISC_ZIP_FILES >> zip_status.tmp

		echo " Making sources.zip ..."
		echo " Making sources.zip ..." > zip_status.tmp
		/usr/local/bin/zip -m sources $SOURCES_ZIP_FILES > zip_status.tmp
		
		echo " Making objects.zip ..."
		echo " Making objects.zip ..." >> zip_status.tmp
		/usr/local/bin/zip -m objects $OBJECTS_ZIP_FILES >> zip_status.tmp		
	fi
	
	mv -f zip_status.tmp zip_status.log
	
	echo " "
	echo " Zip files done !"
	echo " "
	exit
fi



#-------------------------------------#
# Help script section
#-------------------------------------#
if [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Version
fi


#---------------------
# Usage
#---------------------
if [ $CMD_SCRIPT = Usage ]
then
	echo "  $SCRIPT_SGT_NAME	$SCRIPT_SGT_USAGE"
	exit
fi

#---------------------
# Version
#---------------------
echo " "
echo "-------------------------------------"
echo " '$SCRIPT_SGT_NAME' (release "$SCRIPT_SGT_VER") for SGT "$SGT_VER". Last update by "$SCRIPT_SGT_USER" on "$SCRIPT_SGT_DATE
echo "  $SCRIPT_SGT_USAGE"
if [ $CMD_SCRIPT = Version ]
then
	exit
fi

#---------------------
# Simple help
#---------------------
if [ $CMD_SCRIPT = Help ]
then
	echo "-------------------------------------"
	echo " $SCRIPT_SGT_NAME [-OPTION]"
	echo " "
	echo "  -lib  Zip also libraries in libraries.zip"
	echo " "
	echo "  -h | --help  Display this help "
	echo "  -hm          Display more help about SGT" $SGT_VER
	echo "  -v           Display the version of the current script"
fi

#---------------------
# More help
#---------------------
if [ $CMD_SCRIPT = More ]
then
	make -f ../mak/gen.mak help USE_TARGET_FILE=OFF
fi



#-------------------------------------#
# Error messages section
#-------------------------------------#
if [ $CMD_SCRIPT = Error ]
then
	if [ $MSG_ERROR_SCRIPT = 0 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : The root path is not a valid SGT directory. Please check for the \MAK sub-directory"
	fi
	if [ $MSG_ERROR_SCRIPT = 1 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : $1 is an unknown option !"
	fi
	echo " "
	echo " Check your command line or see help section -> $SCRIPT_SGT_NAME -h"
fi
